package repository

import (
	"context"
	"demo/domain/model"
	"gorm.io/gorm"
)

type compressCodeRepository struct {
	db *gorm.DB
}

func (r *compressCodeRepository) BatchSave(ctx context.Context, entity []model.CompressCode) error {
	return r.db.WithContext(ctx).Create(&entity).Error
}

func (r *compressCodeRepository) GetLatestAvailableCompressCode(ctx context.Context) (*model.CompressCode, error) {
	var compressCode model.CompressCode
	err := r.db.WithContext(ctx).Where("code_status = 1 AND deleted = 0").First(&compressCode).Error
	return &compressCode, err
}

func (r *compressCodeRepository) UpdateCodeStatus(ctx context.Context, entity *model.CompressCode) error {
	return r.db.WithContext(ctx).Model(entity).Select("code_status").Updates(entity).Error
}

// NewCompressCodeRepository 创建一个新的 Repository 实例
func NewCompressCodeRepository(db *gorm.DB) model.CompressCodeRepository {
	return &compressCodeRepository{db: db}
}
